cmake_minimum_required(VERSION 3.10)
project(YourProjectName)

set(CMAKE_C_COMPILER "sdcc")
# set(CMAKE_CXX_COMPILER "sdcc")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -g")
set(BUILD_DIR ${CMAKE_SOURCE_DIR}/output/src)
set(INC_DIR ${CMAKE_SOURCE_DIR}/inc)

set(TEST_DIR ${CMAKE_SOURCE_DIR}/output/test)

set(HEX_DIR ${CMAKE_SOURCE_DIR}/output)

include_directories(${INC_DIR})

file(GLOB MY_SOURCE_DIR
    ./src/*.c)

file(GLOB MY_TEST_DIR 
    ./main.c 
    ./test/*.c)

# file(GLOB MY_TEST_DIR 
#     ./test/18-uart.c)

# 初始化一个空的变量来存储修改后的文件路径
set(MY_REL_FILES "")

function(mytest)
    execute_process( COMMAND mkdir -p ${BUILD_DIR} )
    execute_process( COMMAND mkdir -p ${TEST_DIR} )

    foreach(SRC_FILE ${MY_SOURCE_DIR})

        message("${SRC_FILE}")
        get_filename_component(BASENAME ${SRC_FILE} NAME_WE) # 移除扩展名得到基本文件名
        set(REL_FILE "${BASENAME}.rel")
        set(FULL_REL_PATH "${BUILD_DIR}/${REL_FILE}")

        # 输出处理后的文件名
        message("Processing: ${SRC_FILE} -> ${FULL_REL_PATH}")
        list(APPEND MY_REL_FILES ${FULL_REL_PATH})
        message("cmd ${CMAKE_C_COMPILER} -c ${SRC_FILE} -I${INC_DIR} -o ${TEST_REL_PATH}")
        execute_process(
            COMMAND ${CMAKE_C_COMPILER} -c ${SRC_FILE} -I${INC_DIR} -o ${FULL_REL_PATH}
        )
    endforeach()

    message(STATUS "Modified files with .rel extension: ${MY_REL_FILES}")

    message("########################")

    foreach(TEST_FILE ${MY_TEST_DIR})

        message("${TEST_FILE}")
        get_filename_component(BASENAME ${TEST_FILE} NAME_WE) # 移除扩展名得到基本文件名
        set(REL_FILE "${BASENAME}.rel")
        set(TEST_REL_PATH "${TEST_DIR}/${REL_FILE}")
        set(IHX_FILE "${BASENAME}.ihx")
        set(TEST_IHX_PATH "${TEST_DIR}/${IHX_FILE}")
        set(HEX_FILE "${BASENAME}.hex")
        set(TEST_HEX_PATH "${HEX_DIR}/${HEX_FILE}")

        # 输出处理后的文件名
        message("Processing: ${TEST_FILE} -> ${TEST_REL_PATH}")

        message("cmd ${CMAKE_C_COMPILER} -c ${TEST_FILE} -I${INC_DIR} -o ${TEST_REL_PATH}")
        execute_process(
            COMMAND ${CMAKE_C_COMPILER} -c ${TEST_FILE} -I${INC_DIR} -o ${TEST_REL_PATH}
        )

        execute_process(
            COMMAND ${CMAKE_C_COMPILER} ${TEST_REL_PATH} ${MY_REL_FILES} -o ${TEST_IHX_PATH}
        )
        message("cmd packihx ${TEST_IHX_PATH} > ${TEST_HEX_PATH}")
        # execute_process(
        #     COMMAND sleep 5
        # )
        execute_process(
            # COMMAND packihx ${TEST_IHX_PATH} > ${TEST_HEX_PATH}
            COMMAND sh -c "packihx ${TEST_IHX_PATH} > ${TEST_HEX_PATH}"
        )
        message("  ")

    endforeach()

endfunction()

mytest()



# file(GLOB SRC_FILES "main.c" "src/*.c")
# file(MAKE_DIRECTORY ${BUILD_DIR})

# foreach(SRC_FILE ${SRC_FILES})
#     string(REPLACE "${CMAKE_SOURCE_DIR}/" "" REL_PATH "${SRC_FILE}")
#     get_filename_component(REL_DIR "${REL_PATH}" DIRECTORY)
#     file(RELATIVE_PATH REL_DIR "${CMAKE_SOURCE_DIR}/${REL_DIR}" "${BUILD_DIR}")
#     file(MAKE_DIRECTORY "${BUILD_DIR}/${REL_DIR}")
# endforeach()

# add_custom_command(
#     OUTPUT ${BUILD_DIR}main.ihx
#     COMMAND ${CMAKE_C_COMPILER} ARGS ${SRC_FILES} -I${INC_DIR} -o ${BUILD_DIR}main.ihx
#     DEPENDS ${SRC_FILES}
#     COMMENT "Compiling and linking..."
# )

# add_custom_command(
#     OUTPUT ${BUILD_DIR}main.hex
#     COMMAND packihx ARGS ${BUILD_DIR}main.ihx > ${BUILD_DIR}main.hex
#     DEPENDS ${BUILD_DIR}main.ihx
#     COMMENT "Converting .ihx to .hex..."
# )

# add_custom_target(
#     hex ALL
#     DEPENDS ${BUILD_DIR}main.hex
# )

# add_custom_target(
#     clean
#     COMMAND rm -rf ${BUILD_DIR}*
#     COMMAND mkdir -p ${BUILD_DIR}src/led.rel
# )